<?php 

	fta_registerModules(
		array(
			"FTAORM/FTAORMFullCMSContent",
		)
	);
	
	
	class FTAORMFullCmsContentMigrator extends FTAORMFullCMSContent {
		
		//we add in the relationships here so we can easily clone/push

		function __construct($zdb){
			parent::__construct($zdb);
		}
				
		function yankForDocumentCode($id){
			
		
			try {			
				//	first the binary data				
				$this->zdb->delete('binarydata','documentcode=' . $id );
			} catch (Exception $e){} 
			
			try {
				// 	then the many to many				
				$this->zdb->delete('manytomanydata','parentcode=' . $id );
			} catch (Exception $e){} 
			
			try {				
				// then the key value pairs..				
				$this->zdb->delete('multiplekeyvaluepairs','documentcode=' . $id );
			} catch (Exception $e){} 
							
			try {
				//then the metadata
				$this->zdb->delete('cmscontentmetadata','documentcode=' . $id );				
			} catch (Exceptin $e){}
			
			
			try {
				//then the content					
				$this->zdb->delete('content','documentcode=' . $id );								
			} catch (Exception $e){} 
			
		}
		
		
		function migrate($newZdb){
			//this has to be explicitly transactioned
			
			try {
				
				
				// load everything..
				$this->fault();
			
				//flip the database			
				$this->zdb = $newZdb;
				$this->cmscontentmetadata->zdb = $newZdb;
				
				//TODO: add the cms content metadata conenctions 
				
				//update the cmscontent metadata etc..
			
				$this->zdb->beginTransaction();
								
				//now.. check for an existing version or quietly yank? and always save new..
				$pk = $this->getPrimaryKey();
				$this->yankForDocumentCode($this->getPrimaryKey());
			
				
				
				//yank the primary keys
				$this->setPrimaryKey(null);
				$this->cmscontentmetadata->setPrimaryKey(null);
								
				//TODO: yank the bin/mtm binary keys etc..
				
				if (!$this->save($pk)) {
					throw new Exception('Error saving / migrating record');
				}
				
				
				//now the binarydata, we cannot rely on the save mechanisim in the orm to 
				// handle the relationship correctly.
				
				foreach ($this->binarydata as $bd){
					$bd->zdb = $newZdb;
					$pk = $bd->getPrimaryKey();
					$bd->setPrimaryKey(null);
					$bd->save($pk);// do we care about failures here??
				}
				
				foreach ($this->manytomanydata as $md){
					$md->zdb = $newZdb;
					$pk = $md->getPrimaryKey();
					$md->setPrimaryKey(null);
					$md->save($pk);// do we care about failures here??
				}				

				foreach ($this->multiplekeyvaluepairs as $mk){
					$mk->zdb = $newZdb;
					$pk = $mk->getPrimaryKey();
					$mk->setPrimaryKey(null);
					$mk->save($pk);// do we care about failures here??
				}	
								
				// try and commit if we get this far...
				$this->zdb->commit();
				
				return true;
				
			} catch (Exception $e){
				try {
					$this->zdb->rollback();
				} catch (Exception $e){}
				
				return false;
				
			}
		}
		
		
		//
		// function save?? or store?? or publish??
		//
		
		/**
		 * force all of the relationships to be loaded in..
		 */
		function fault() {
			$this->cmscontentmetadata;			
			$this->manytomanydata;
			$this->binarydata;
			$this->multiplekeyvaluepairs;
			

		}
		
		//
	}